import path         from 'path';
import _            from 'lodash';
import express      from 'express';
import cookieParser from 'cookie-parser';
var bodyParser = require('body-parser');

// init express instance
var app = new express();

// http proxy for dev env
if (process.env.NODE_ENV != 'production') {
  require('./proxy')(app,"138.68.41.35");
}else{
  // require('./proxy')(app,"138.68.41.35");
}


app.use(cookieParser());

// serve web resources
if (process.env.NODE_ENV == 'production') {
  // run in production mode
  app.use('/camobile-appstore-statics', express.static(`${__dirname}/../static-dist`));
} else {
  // run in dev mode with webpack
  var webpack = require('webpack');
  var webpackDevMiddleware = require('webpack-dev-middleware');
  var webpackHotMiddleware = require('webpack-hot-middleware');
  var config = require('../webpack/webpack.dev.config');
  console.log(config)
  var compiler = webpack(config);
  app.use(webpackDevMiddleware(compiler, {
    noInfo: true,
    publicPath: "/camobile-appstore-statics"
  }));
  app.use(webpackHotMiddleware(compiler, {
    hot: true
  }));
}

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded




app.get("/apitest", function(req, res) {
  console.log('测试RSAA get');
  res.status(200).json({
    payload: "get list success"
  });
});

app.get("/apitest/*", function(req, res) {
  console.log('测试RSAA get');
  res.status(200).json({
    payload: "get one success"
  });
});

app.post("/apitest", function(req, res) {
  console.log('测试RSAA post');
  console.log(`${JSON.stringify(req.body)}`)
  res.status(200).json({
    payload: "post success"
  });
});

app.put("/apitest/*", function(req, res) {
  console.log('测试RSAA put');
  console.log(`${JSON.stringify(req.body)}`)
  res.status(200).json({
    payload: "put success"
  });
});

app.delete("/apitest/*", function(req, res) {
  console.log('测试RSAA delete');
  res.status(200).json({
    payload: "delete success"
  });
});

require('./app')(app)

app.get("/api/list", function(req, res) {

  console.log('测试RSAA get');
  res.status(200).json({
    totalPages: 3,
    payload: [
      {id:1,name:'saa'},
      {id:2,name:'adfs'},
      {id:3,name:'dsfdas'},
      {id:4,name:'sdfdsf'},
      {id:5,name:'sdf'},
      {id:6,name:'d'},
      {id:7,name:'df'},
      {id:8,name:'adf'},
      {id:9,name:'sdf'},
      {id:10,name:'erq'},
      {id:11,name:'saa'},
      {id:12,name:'adfs'},
      {id:13,name:'dsfdas'},
      {id:14,name:'sdfdsf'},
      {id:15,name:'sdf'},
      {id:16,name:'d'},
      {id:17,name:'df'},
      {id:18,name:'adf'},
      {id:19,name:'sdf'},
      {id:20,name:'erq'}
      ]
  });
});

// default port 3000
var port = process.env.PORT || 3000;

app.listen(port, function(error) {
  if (error) {
    console.error(error);
  } else {
    console.info("==> 🌎  Listening on port %s. Open up http://localhost:%s/ in your browser.", port, port);
  }
});
